#!/bin/bash

echo -e "\033[1;41;33m[ sample: ./3.shell/mysql-import.sh -d supreme_dev ]\033[0m"

host=127.0.0.1
port=3306
username=root
password=111111
databases=''
target=''
output='./temp-data/'

while getopts "h:p:u:a:d:o:t:" opt; do
  case $opt in
    h)
        host=$OPTARG
        ;;
    p)
        port=$OPTARG
        ;;
    u)
        username=$OPTARG
        ;;
    a)
        password=$OPTARG
        ;;
    d)
        databases=$OPTARG
        ;;
    t)
        target=$OPTARG
        ;;
    o)
        output=$OPTARG
        ;;
    \?)
        echo "invalid arg"
        exit 1
        ;;
  esac
done

if [ -z "$databases" ]; then
  echo '-d [ supreme_dev | quartz_config | xxl_job | nacos_config ]'
  exit 1
fi

if [ -z "$target" ]; then
  output_file="$output$databases.sql"
else
  output_file="$output$target"
fi

import_shell="-h$host -P$port -u$username -p$password $databases < $output_file"
echo "mysql $import_shell"

mysql -h"$host" -P"$port" -u"$username" -p"$password" "$databases" < "$output_file"